


$(document).ready(function () {
    $("select").bind("change", function () {

        var clickItem = $(this).attr("id");

        if (clickItem == "group1") {
            $("#group2").empty();
            $("#group3").empty();
            $("#group2").addClass("none");
            $("#group3").addClass("none");
            $("#group2").append("<option value = ''>请选择分类</option>");
            $("#group3").append("<option value = ''>请选择分类</option>");
            var parentId1 = $(this).val();
            if (parentId1.length > 0) {
                loadGroup($("#group2"), parentId1, 0);
            }
        }
        else if (clickItem == "group2") {

            $("#group3").empty();
            $("#group3").addClass("none");
            $("#group3").append("<option value = ''>请选择分类</option>");

            var parentId1 = $("#group1").val();
            var parentId2 = $(this).val();
            if (parentId2.length > 0) {
                loadGroup($("#group3"), parentId1, parentId2);
            }
        }
    });

    $("#btnOk").bind("click", function () {

        //将内容添加到表单
        $("#description").text(editor.html());

        //表单验证
        if (!call_submit("form")) {
            return;
        }

        var ks = $("#keywords").val();
        ks = ks.replace(/，/g, ",");
        var kss = ks.split(",");

        if (kss.length > 10) {
            toast.show("关键词不能超过10个");
            $("#keywords").focus();
            return;
        }

        for (var i in kss) {
            if (kss[i].length > 15) {
                alert("你的关键词“" + kss[i] + "”超过了15个字符！");
                $("#keywords").focus();
                return;
            }
        }



        var id = getQueryString("id");
        if (id.length == 0) {
            //添加产品时验证
            if ($("#pic1").val().length < 1) {
                toast.show("第一张图片不能为空");
                return;
            }
        }

        $("form").submit();

    });

});

//加载分组
function loadGroup(groupNum, parentId1, parentId2) {

    var optionHtml = "<option value = {id}>{name}</option>";

    ajax.getGoupList(parentId1, parentId2, function (json) {

        if (json.code != 0) {
            return;
        }

        //        toast.show(JSON.stringify(json));

        var list = json.item;
        for (var i in list) {
            $(groupNum).append(optionHtml.replace(/{id}/g, list[i].id).replace(/{name}/g, list[i].name));
        }
        $(groupNum).removeClass("none");

    });
}

